<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="Cache-Control" content="no-cache,must-revalidate">
	<link rel="stylesheet" href="<s:url value="/css/style_new.css"/>">
	<link rel="stylesheet" href="<s:url value="/css/style.css"/>">
	<script src="<s:url value="/script/jquery_last.js"/>" type="text/javascript"></script>
	<script type="text/javascript" src="<s:url value="/script/easyajax.js"/>"></script>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0">
<br/>
<table width="96%" border="0" cellspacing="0" cellpadding="0"
	align="center">
	<tr>
		<td width="20%" >
		<input type="button" name="bt" class="buttoncom4"  value="确 定" onclick="sure()"/>
		<input type="button" name="bt" class="buttoncom4"  value="关 闭" onclick="window.close();"/>
		</td>
	</tr>
</table>
<br/>
<table width="96%" border="0" cellspacing="0" cellpadding="0"
	align="center" class="tablelistnew">
	<tr>
		<td width="20%">快速定位</td>
		<td colspan="5" style="text-align:left"><input type="text" 
		name="tSearch" value="请输入人员姓名或者帐号" onfocus="this.value=''" onkeydown="qSearch(this.value)"
	    style="width:240px"/><font color="red">（注:可通过MIS帐号或姓名进行快速定位）</font></td>
	</tr>
	<tr>
		<td width="25%">一级部门</td>
		<td width="25%"><a href="#" onclick="setSubDeptSelect()"><u>二级部门</u></a></td>
		<td width="25%"><a href="#" onclick="setPersonSelect()"><u>人员</u></a></td>
		<td width="25%">已选择人员<a href="#" onclick="clearPerson()"><u>&lt;清空&gt;</u></a></td>
	</tr>
	<tr>
		<td>
			<select name="depts" id="depts" class="selStyle"
			property="id" onclick="clearDept()" labelProperty="name" multiple="multiple">
		</td>
		<td>
			<select name="subDepts" id="subDepts" class="selStyle"
			property="id" onclick="clearSubDept()" labelProperty="name" multiple="multiple">
		</td>
		<td>
			<select name="persons" id="persons" class="selStyle"
			property="personID" labelProperty="showName" multiple="multiple"
			ondblclick="addPerson()">
		</td>
		<td>
			<select name="selectedPerson" id="selectedPerson" class="selStyle"
			 multiple="multiple" ondblclick="deletePerson()">
		</td>
	</tr>
</table>
</body>
</html>
<script>
<!--
/*初始化页面数据*/
initPage();

/*定义*/
var deptAjax=new AjaxObj();
var subDeptAjax=new AjaxObj();
var personAjax=new AjaxObj();

var corpID=$("corp");
var deptID=$("depts");
var subDeptID=$("subDepts");
var showPersons=$("persons");
var selPerson=$("selectedPerson");

/*清除*/
function clearDept(){
	clearList(subDeptID);
	clearList(showPersons);
}
function clearSubDept(){
	clearList(showPersons);
}
function clearPerson(){
	clearList(selPerson);
}
/*设置部门*/
function setDeptSelect(corpIDVal){
	if (corpIDVal && corpIDVal.value!=""){
		clearList(deptID);
		getDepts(corpIDVal);
	}else{
		alert("没有公司数据");
	}
}
/*设置子部门*/
function setSubDeptSelect(){
	if (deptID && deptID.value!=""){
		clearList(subDeptID);
		getSubDepts(deptID.value);
	}else{
		alert("您没有选择部门");
	}
}
/*设置人员*/
function setPersonSelect(filter){
	var obj=subDeptID;
	var isFirst="N";

	if (subDeptID && subDeptID.value!=""){
		obj=subDeptID;
	}else if (deptID && deptID.value!=""){
		obj=deptID;
		isFirst="Y";
	}

	if (obj && obj.value!=""){
		clearList($("persons"));
		$("persons").labelProperty="personName";
		getPersons(obj.value,'',isFirst);
	}else{
		alert("您没有选择部门");
	}
}

/*清除列表*/
function clearList(sel){
	if(sel&&sel.options&&sel.options.length>0){
		for(var i=sel.options.length;i>0;i--){
			sel.removeChild(sel.options[i-1]);
		}
	}
}
/*获取公司下部门*/
function getDepts(corpID){	
	callSynchronized("<s:url value="/organ/findDeptsBy.action"/>?corpID="+corpID,function(XML){//同步调用
		deptAjax=new AjaxObj();
		deptAjax.xml=XML.responseXML;
		deptAjax.menuId="depts";
	    deptAjax.createMenu();
	});
}
/*获取子部门*/
function getSubDepts(orgID){	
	callSynchronized("<s:url value="/organ/findAllOrgsBy.action"/>?orgID="+orgID,function(XML){//同步调用
		deptAjax=new AjaxObj();
		deptAjax.xml=XML.responseXML;
		deptAjax.menuId="subDepts";
	    deptAjax.createMenu();
	});
}
/*获取部门人员*/
function getPersons(orgID,filter,isFirst){	
	var url="<s:url value="/organ/findPersonsByOrg.action"/>?";
	var tVal="isFirst="+isFirst;
	if (orgID && orgID!=null && orgID!=""){
		if (tVal==""){
			tVal="orgID="+orgID;
		}else{
			tVal+="&orgID="+orgID;
		}
	}
	if (filter && filter!=""){
		if (tVal==""){
			tVal="filter="+filter;
		}else{
			tVal+="&filter="+filter;
		}
	}
	url+=tVal;
	callSynchronized(url,function(XML){//同步调用
		deptAjax=new AjaxObj();
		deptAjax.xml=XML.responseXML;
		deptAjax.menuId="persons";
	    deptAjax.createMenu();
	});
}
/*新增关联产品*/
function addPerson(){
	
	for(i=$("persons").options.length-1;i>-1;i--){
		if($("persons").options[i].selected==true){
		    var option=document.createElement("option");
		    objSel=$("persons").options[i];
		    var idVal=$("persons").options[i].value;
		    var textVal=$("persons").options[i].text;
		//	var selId=idVal.substring(0,idVal.indexOf("/"));
			var selId=idVal;
			
	    	//option.value=idVal.substring(0,idVal.indexOf("/"));
	    	option.value=idVal;
	    	/*if (textVal.indexOf("/")!=-1){
	    		option.text=textVal.substring(0,idVal.indexOf("/"))+"("
	    			+idVal.substring(idVal.indexOf("/")+1,idVal.length)+")";
	    	}else{
	    		option.text=textVal+"("
    			+idVal.substring(idVal.indexOf("/")+1,idVal.length)+")";
	    	}*/
	    	option.text=textVal;
	    	isAdd=true;
	    	for(j=$("selectedPerson").options.length-1;j>-1;j--){
		    	selectedObj=$("selectedPerson").options[j];
	    		if (selId==selectedObj.value){
		    		isAdd=false;
		    		break;
	    		};
	    	}
			if (isAdd){
				$("selectedPerson").add(option);
			}else{
				alert("您已选择这个人员。");
			};
		}
	}
}
/*删除关联产品*/
function deletePerson(){
	for(i=$("selectedPerson").options.length-1;i>-1;i--){
		if($("selectedPerson").options[i].selected==true){
		    var option=document.createElement("option");
	    	option.value=$("selectedPerson").options[i].value;
	    	option.text=$("selectedPerson").options[i].text;
			$("selectedPerson").remove(i);
		}
	}
}
/*保存并返回*/
function sure(){
	var obj=$("selectedPerson");
	var len=$("selectedPerson").options.length;
	if (len<1){
		if (confirm("您未选择人员.确定要退出吗？")){
			window.close();
		}else{
			return;
		}
	}
	var arrReturn=new Array();
	var returnVal="";
	var returnName="";
	for (i=0;i<len;i++){
		if (returnVal==""){
			returnVal=obj.options[i].value;
			returnName=obj.options[i].text;	
		}else{
			returnVal+=","+obj.options[i].value;
			returnName+=","+obj.options[i].text;	
		}		
	}
	arrReturn[0]=returnVal;
	arrReturn[1]=returnName;	
	window.returnValue=arrReturn;
	window.close();
}
/*初始化已选择数据*/
function initPage(){
	var objVal='<s:property value="returnObj"/>';
	var objName='<s:property value="returnObjName"/>';
	var corpID='<s:property value="corpID"/>';

	//初始部门数据
	setDeptSelect(corpID);
	
	var objVals=window.dialogArguments.document.all[objVal];
	var objnames=window.dialogArguments.document.all[objName];
	if(objVals){
		var val=objVals.value;
		var name=objnames.value;
		if (val!=""){
			var ids=val.split(",");
			var names=name.split(",");
			if (ids.length && ids.length>0){
				for (i=0;i<ids.length;i++){
				    var option=document.createElement("option");
			    	option.value=ids[i];
			    	option.text=names[i];
					$("selectedPerson").add(option);
				}
			}
		}
	}
}

function qSearch(filter){
	if (event.keyCode==13){
		if (filter!=""){
			clearList($("persons"));
			$("persons").labelProperty="personName";
			var corpID='<s:property value="corpID"/>';
			getPersons(corpID,filter);
		}else{
			return false;
		}
	}else{
		return false;
	}
}
-->
</script>


